

#ifndef _STREAM_BUFFER_H_
#define _STREAM_BUFFER_H_

#ifdef __cplusplus
extern "C"
{
#endif

	/**
		2022年5月10日
		v0.0.0

	 */
	/*
	 --- Lab module ---
	 Name: Stream buffer.
	 data:2022-05-10
	 author:ZC
	 version:v0.0.0
	 describe:
			function segment.

	record 2022-05-24:
		`Fix alignment function.When input multiple of 4,consume 4 extra bytes.

	record 2022-05-25:
		`Optimization: function to define "usStreamBufferNumberOfItemsGet(StreamBufferDef* pxStream)"

	*/

#include "types.h"

typedef struct
{
	uint16 ucNfItems;

	uint16 usSize;
	uint8 *pcBuffer;

	uint16 ucRx;
	uint16 ucWr;

} StreamBufferDef;

typedef enum
{
	eSTREAM_NOT_ERR = 0,	   // not anything error.
	eSTREAM_SPACE_NOT_ENOUGH,  // space not enough
	eSTREAM_NOT_ALIGNMEMT,	   // stream buffer not alignment.
	eSTREAM_BUFFER_IS_INVALID, // Stream buffer is invalid.
	eSTREAM_DYNAMIC_MEM_ERROR,
} StreamStatusDef;

/*#############################################*/
/* Sream buffer create. */ /*!disuse , not dynamic memory */
StreamStatusDef xStreamBufferStaticCreate(StreamBufferDef *pxStream, uint8 *pcBuf, uint16 usSzie);

/* use dynamic memory create stream buffer. */
StreamStatusDef xStreamBufferCreate(StreamBufferDef *pxStream, uint16 usSzie);

/* User dynamic memory and this. */
StreamStatusDef vStreamBufferDestroy(StreamBufferDef *pxStream);

/* Stream buffer free space get. */
uint16 usStreamBufferFreeGet(StreamBufferDef *pxStream);

/* Get stream buffer number of items get. Input stream object pointer. */
#define usStreamBufferNumberOfItemsGet(pxStream) ((pxStream)->ucNfItems)

/* Stream buffer write one item. */
StreamStatusDef xStreamBufferWrite(StreamBufferDef *pxStream, void *pcWriteBuf, uint16 usSzie);

/* Peek next item size. */
uint16 usStreamBufferGetNextSize(StreamBufferDef *pxStream);

/* Read a item form stream buffer , simultaneously. */
uint16 usStreamBufferRead(StreamBufferDef *pxStream, void *pcReadBuf);

/* Peek item , not delete data.(not verification.) */
uint16 usStreamBufferPeek(StreamBufferDef *pxStream, uint8 *pcPeekBuf, uint16 usNumber);

#ifdef __cplusplus
}
#endif

#endif /* endif */
